<?php

	$action = fgsBase::getParam(0);
	$id = (int)fgsBase::getParam(1);
	$form = null;

	// ---------------------------------------------------------------
	// ADD or EDIT a user 
	// ---------------------------------------------------------------
	if ($action == 'add' || $action == 'edit') {
		$form = new htmlForm('user');
		
		if ($form->isCancel())
			fgsBase::redirect('admin_players');
		
		if ($action == 'add') {
			$form->addSection(tr('Add as user'));
		} else {
			$user = fgsUsers::getById($id);
			if (!$user)
				fgsBase::redirect('admin_players');
				
			$form->addSection(sprintf(tr('Edit user "%s"'), $user['login']));
			
			$form->default_values['login'] = $user['login'];
			$form->default_values['email'] = $user['email'];
			$form->default_values['website'] = $user['website'];
			$form->default_values['is_deleted'] = $user['is_deleted'] ? 1 : 0;
			$form->default_values['is_admin'] = $user['is_admin'] ? 1 : 0;
		}

		$form->addField(tr('Login'), 'login', 'text', true);
		$form->addField(tr('Password'), 'password', 'text');
		$form->addField(tr('Email'), 'email', 'text', true);
		$form->addField(tr('Website'), 'website', 'text');
		$form->addField(tr('Deleted'), 'is_deleted', 'checkbox');
		$form->addField(tr('Admin'), 'is_admin', 'checkbox');
		$form->addSubmit(tr('Submit'));

		// add user if post is good
		if ($form->isPost() && $form->isValid()) {
			$login = $form->get('login');
			$email = $form->get('email');
			$website = $form->get('website');
			$is_deleted = $form->get('is_deleted') ? true : false;
			$is_admin = $form->get('is_admin') ? true : false;

			$password = $form->get('password');
			if (!empty($password))
				$password = md5($password);
			else
				$password = $user['password'];

			$avatar = $user['avatar'];
			
			if ($action == 'add')
				fgsUsers::add($login, $password, $email, $website, $avatar, $is_deleted, $is_admin);
			else 
				fgsUsers::update($id, $login, $password, $email, $website, $avatar, $is_deleted, $is_admin);

			fgsBase::redirect('admin_players');
		}


	// ---------------------------------------------------------------
	// UNDELETE an user
	// ---------------------------------------------------------------
	} else if ($action == 'show') {
		fgsUsers::enable($id, 1);
		fgsBase::redirect('admin_players');
		
	// ---------------------------------------------------------------
	// DELETE an user
	// ---------------------------------------------------------------
	} else if ($action == 'hide') {
		fgsUsers::enable($id, 0);
		fgsBase::redirect('admin_players');

	}
	
?>
